package com.harmony.pilot.completion.lsp;

import com.harmony.pilot.completion.service.CompletionService;
import com.harmony.pilot.completion.service.DiagnosticService;
import com.harmony.pilot.completion.service.NavigationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

/**
 * LSP服务器控制器
 * 提供HTTP接口用于启动和管理LSP服务器
 */
@Slf4j
@RestController
@RequestMapping("/lsp")
@ConditionalOnProperty(name = "lsp.enabled", havingValue = "true", matchIfMissing = false)
public class LanguageServerController {
    
    private final LanguageServerLauncher launcher;
    
    @Value("${lsp.enabled:false}")
    private boolean lspEnabled;
    
    public LanguageServerController(
            CompletionService completionService,
            NavigationService navigationService,
            DiagnosticService diagnosticService) {
        this.launcher = new LanguageServerLauncher(
            completionService,
            navigationService,
            diagnosticService
        );
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public String health() {
        return "LSP Server is " + (lspEnabled ? "enabled" : "disabled");
    }
    
    /**
     * 获取LSP服务器信息
     */
    @GetMapping("/info")
    public LanguageServerInfo getInfo() {
        LanguageServerInfo info = new LanguageServerInfo();
        info.setEnabled(lspEnabled);
        info.setName("OpenHarmony Language Server");
        info.setVersion("1.0.0");
        info.setCapabilities(new String[]{
            "textDocument/completion",
            "textDocument/definition",
            "textDocument/references",
            "textDocument/implementation",
            "textDocument/hover",
            "textDocument/diagnostics",
            "workspace/symbol"
        });
        return info;
    }
    
    /**
     * LSP服务器信息
     */
    public static class LanguageServerInfo {
        private boolean enabled;
        private String name;
        private String version;
        private String[] capabilities;
        
        public boolean isEnabled() { return enabled; }
        public void setEnabled(boolean enabled) { this.enabled = enabled; }
        
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        
        public String getVersion() { return version; }
        public void setVersion(String version) { this.version = version; }
        
        public String[] getCapabilities() { return capabilities; }
        public void setCapabilities(String[] capabilities) { this.capabilities = capabilities; }
    }
}

